package test;

import com.sun.org.apache.xpath.internal.SourceTree;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class no1 {

    /**
     * hash数据结构操作
     */
    @Test
    public void test1(){
        //1.获取连接
        Jedis jedis = new Jedis();
        //2.操作
        //存储hash
        jedis.hset("user","name","lisi");
        jedis.hset("user","age","23");
        jedis.hset("user","gender","male");

        //获取hash
        String hget = jedis.hget("user", "name");
        String hget2 = jedis.hget("user", "age");
        String hget3 = jedis.hget("user", "gender");

        System.out.println(hget+"\t"+hget2+"\t"+hget3);

        //获取hash的所有map中的数据
        Map<String,String> map = jedis.hgetAll("user");

        //keySet
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            //获取value
            String value = map.get(key);
            System.out.println(key+"\t"+value);
        }

        //3.关闭连接
        jedis.close();
    }

    /**
     * list数据结构操作允许存入重复数据
     */
    @Test
    public void test2(){
        //1.获取连接
        Jedis jedis = new Jedis();
        //2.操作
        //存储list
        jedis.lpush("mylist","a","b","c");      //从左边存
        jedis.rpush("mylist","a","b","c");      //从右边存

        //list范围获取
        List<String> mylist = jedis.lrange("mylist", 0, -1);//表示获取所有

        System.out.println(mylist);

        //list弹出
        String mylist1 = jedis.lpop("mylist");//左边弹出c
        System.out.println(mylist1);

        String mylist2 = jedis.rpop("mylist");//右边弹出c
        System.out.println(mylist1);

        //list范围获取
        List<String> mylist3 = jedis.lrange("mylist", 0, -1);//表示获取所有

        System.out.println(mylist3);

        //3.关闭连接
        jedis.close();
    }

    /**
     * set数据结构操作不允许存入重复数据的无序集合
     */
    @Test
    public void test3(){
        //1.获取连接
        Jedis jedis = new Jedis();
        //2.操作
        //存储set
        jedis.sadd("myset","java","php","c++");

        //获取set
        Set<String> myset = jedis.smembers("myset");
        System.out.println(myset);

        //3.关闭连接
        jedis.close();
    }

    /**
     * sortedset数据结构操作不允许存入重复数据的有序数据
     */
    @Test
    public void test4(){
        //1.获取连接
        Jedis jedis = new Jedis();
        //2.操作
        //存储sortedet
        jedis.zadd("mysortedet",3,"亚索");
        jedis.zadd("mysortedet",2,"瑞文");
        jedis.zadd("mysortedet",9,"提莫");

        //获取sortedet
        Set<String> mysortedet = jedis.zrange("mysortedet", 0, -1);
        System.out.println(mysortedet);

        //3.关闭连接
        jedis.close();
    }

    @Test
    public void test5(){
        //0.创建一个配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(50);
        config.setMaxIdle(10);
        //1.创建jedis连接池对象
        JedisPool jedisPool = new JedisPool(config,"localhost",6379);

        //2.获取连接
        Jedis jedis = jedisPool.getResource();
        //3.使用
        jedis.set("hehe","heihei");

        //4.关闭，归还到连接池中
        jedis.close();

    }
}
